﻿using System.Data.Entity;
using System.Linq;

namespace Metro.Kashikoi.Service.Common.Entity
{
    /// <summary>
    /// This class provides the base class of <c>IUnitOfWork</c>.
    /// </summary>
    public abstract class ContextBase : DbContext, IUnitOfWork
    {
        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="ContextBase"/> class.
        /// </summary>
        /// <param name="conn">The conn.</param>
        public ContextBase(string conn)
            : base(conn)
        { } 

        #endregion

        #region IUnitOfWork Members

        /// <summary>
        /// Deletes the specified entity.
        /// </summary>
        /// <typeparam name="TEntity">The type of the entity.</typeparam>
        /// <param name="entity">The entity.</param>
        public virtual void Delete<TEntity>(TEntity entity) where TEntity : class
        {
            base.Set<TEntity>().Remove(entity);
        }

        /// <summary>
        /// Adds the specified entity.
        /// </summary>
        /// <typeparam name="TEntity">The type of the entity.</typeparam>
        /// <param name="entity">The entity.</param>
        public virtual void Add<TEntity>(TEntity entity) where TEntity : class
        {
            base.Set<TEntity>().Add(entity);
        }

        /// <summary>
        /// Removes the specified entity.
        /// </summary>
        /// <typeparam name="TEntity">The type of the entity.</typeparam>
        /// <param name="entity">The entity.</param>
        public virtual void Remove<TEntity>(TEntity entity) where TEntity : class
        {
            base.Set<TEntity>().Remove(entity);
        }

        /// <summary>
        /// Attaches the specified entity.
        /// </summary>
        /// <typeparam name="TEntity">The type of the entity.</typeparam>
        /// <param name="entity">The entity.</param>
        public virtual void Attach<TEntity>(TEntity entity) where TEntity : class
        {
            base.Set<TEntity>().Attach(entity);
        }

        /// <summary>
        /// Gets the query.
        /// </summary>
        /// <typeparam name="TEntity">The type of the entity.</typeparam>
        /// <returns>
        /// The query.
        /// </returns>
        public virtual IQueryable<TEntity> GetQuery<TEntity>() where TEntity : class
        {
            return base.Set<TEntity>();
        }

        #endregion

        #region Dispose

        /// <summary>
        /// Disposes the context. The underlying <see cref="T:System.Data.Objects.ObjectContext"/> is also disposed if it was created
        /// is by this context or ownership was passed to this context when this context was created.
        /// The connection to the database (<see cref="T:System.Data.Common.DbConnection"/> object) is also disposed if it was created
        /// is by this context or ownership was passed to this context when this context was created.
        /// </summary>
        /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
        protected override void Dispose(bool disposing)
        {
            this.Configuration.LazyLoadingEnabled = true;
            base.Dispose(disposing);
        } 

        #endregion
    }
}
